package com.huichenghe.xinlvsh01.CustomView;

import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.provider.CalendarContract;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

import com.huichenghe.xinlvsh01.CommonUtilss.CommonUtils;
import com.huichenghe.xinlvsh01.R;
import com.huichenghe.xinlvsh01.Utils.FormatUtils;

// 自定义 view,需要实现 一个显式的构造函数，重写 onDraw 方法，一切的操作都在该方法上进行
public class HeartReatDetailsView extends ImageView {
	private Context context;
	
	private static final String TAG = "SportDetailsView";
	
	private static final int textSizeDp = 22;
	private static final int textSizeDpSmall = 16;
	private float textH;
	private float textSmall;






	int Color_A = 0xFFFFA6A2;		// 第一颜色块颜色
	int Color_B = 0xFF61CDB5;		// 第二颜色块颜色
	int Color_C = 0xFFfFA6A2;		// 同第一一样
	int[] ColorArray = {Color_A, Color_B, Color_C,};	// 将颜色值放入数组
	String[] ColorName = {getResources().getString(R.string.danger_text),//	将色块名称放入数组
						  getResources().getString(R.string.normal_text),
						  getResources().getString(R.string.danger_text),};

	String[] yScale = {String.valueOf(220), String.valueOf(180), String.valueOf(50)};
	
	int Color_QD = 0xffffb100;		// 心率曲线
	
	
	private class stData {
//		int beginTime, endTime;
		List<Integer> sport_rate_Arrary = new ArrayList<Integer>();
	}
	
	stData mAllData = new stData();
	int mMinTime, mMaxTime, mMaxYDQDValue = 5;
	int mMaxRateValue = 220; // 最大心率，y轴上的坐标


	/**
	 * 显式构造方法
	 * @param context
	 */
	public HeartReatDetailsView(Context context) {
		super(context);
		this.context = context;

		init();
	}
	
	public HeartReatDetailsView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		init();
	}

	public HeartReatDetailsView(Context context, AttributeSet attrs,
								int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		this.context = context;
		init();
	}
	
	private void init() {
		// 获取22dp字体的像素宽度
		textH = CommonUtils.getTextViewWidth(getContext(), "A", textSizeDp);
		textSmall = CommonUtils.getTextViewWidth(getContext(), "A", textSizeDpSmall);
		
//		mMinTime = 30* 60;
//		mMaxTime = 33*60;
//		for (int i = 0; i < 60; i++) {
//			mAllData.sport_rate_Arrary.add(i * 30);
//		}
//		
//		for (int i = 0; i < 30; i++) {
//			mAllData.sport_ydqd_Arrary.add(i % 5);
//		}
//		
//		mMaxRateValue = 2000;
//		mMaxYDQDValue = 5;
	}


	/**
	 * 通过此方法设置数据
	 * @param beginTime			// 开始时间
	 * @param endTime			// 结束时间
	 * @param rates				// 心率字符串
	 * @param maxRate			// 最大心率
	 */
	public void setData(int beginTime, int endTime, String rates, float maxRate) {
		
		
		byte[] rateData = FormatUtils.hexString2ByteArray(rates);		// 将十六进制字符串转化为byte数组，
		float tmp = 0;
		for (int r = 0; r < rateData.length; r++)						// 遍历数组，将数据添加进集合中
		{
			if (rateData[r] == (byte)0xff)
			{					// 对无效数据进行过滤，此处暂不进行过滤
				rateData[r] = (byte)0x00;		// 0xff为无效数据，当0处理
			}
			
			tmp = (float)(rateData[r] & 0xff);
			mAllData.sport_rate_Arrary.add((int) tmp);		// 将心率数据添加到集合中
		}
		

//		mMaxRateValue = (int) maxRate;
		
		mMinTime = beginTime;		// 开始时间
		mMaxTime = endTime;			// 结束时间
		
		
		Log.e(TAG, TAG + "setData beginTime = "+beginTime);
		Log.e(TAG, TAG + "setData endTime = "+endTime);
		Log.e(TAG, TAG + "setData sport_rate_Arrary = " + mAllData.sport_rate_Arrary.size());
	}


	/***
	 * 心率高度为10~220
	 * 画图时分21格，最上边一个为4格，中间为13格，最下边为4格，按此等分画出背景色块
	 * @param canvas
	 */

	int countSolid = 21;

	@Override
	public void onDraw(Canvas canvas) {
	//if (mAllData.sport_rate_Arrary.size() > 2) 
		{
			subDraw(canvas);
		}
		super.onDraw(canvas);
	}
	
	private void subDraw(Canvas canvas) {
		
		Paint paint = new Paint();		 // 画笔
		paint.setAntiAlias(true);		 // 消除锯齿
		paint.setStyle(Paint.Style.FILL);// 设置实心
		paint.setStrokeWidth(1);		 // 设置边宽度
		paint.setTextSize(textH);		 // 设置text的大小
		
		float width = getMeasuredWidth();		// 获取自身宽度
		float height = getMeasuredHeight();		// 获取自身高度
//		float sy_begin = (height - 3f * textH);	// 色块区域的高度
		
		// 画五彩色阶
		float blockH = height / countSolid;		// 每一格的高度
//		for (int i = 0; i < ColorArray.length; i++) {

		//  画出第一个色块
			paint.setColor(ColorArray[0]);
			canvas.drawRect(0, 0 * blockH, width, blockH * 4, paint);
			paint.setColor(Color.RED);
			canvas.drawText(ColorName[0], 120, (2 * blockH) + (blockH / 2), paint);
			paint.setColor(Color.WHITE);
			paint.setTextSize(textSmall);
			canvas.drawText(yScale[0], 20, blockH, paint);
//		}

		// 画出第二个色块
		paint.setColor(ColorArray[1]);
		canvas.drawRect(0, 4 * blockH, width, blockH * 17, paint);
		paint.setColor(Color.WHITE);// 画出色块大字
		paint.setTextSize(textH);
		canvas.drawText(ColorName[1], 120, 11 * blockH, paint);
		paint.setColor(Color.WHITE);
		paint.setTextSize(textSmall);
		canvas.drawText(yScale[1], 20, blockH * 5, paint);	// 画出180
		canvas.drawText(yScale[2], 20, (blockH * 17) - blockH/3, paint); // 画出50

		// 画出第三个色块
		paint.setColor(ColorArray[2]);
		canvas.drawRect(0, 17 * blockH, width, blockH * 21, paint);
		paint.setTextSize(textH);
		paint.setColor(Color.RED);
		canvas.drawText(ColorName[0], 120, (20 * blockH) - (blockH / 2), paint);

		drawRate(canvas, paint, width, height);		// 画心率曲线
//		drawBottom(canvas, paint, width, height);
	}


		// 画运动心律曲线
	private void drawRate(Canvas canvas, Paint paint, float width, float height)
	{
		paint.setColor(Color.WHITE);			// 心率曲线的颜色
		paint.setStrokeWidth(3);				// 心率曲线的宽度

		float eachY = height / mMaxRateValue;   // 计算每一个垂直的像素，内涵的数值，即y轴的刻度
		float step_x = 0, step_y = 0;
		float last_step_x = -1, last_step_y = -1;

		float allCount = mAllData.sport_rate_Arrary.size();			// 心率数据长度
		float eachDistanceW = width / allCount; // 计算有多少个刻度	x轴上每个像素的数值

		int[] showData = new int[(int)allCount];
//		int eachValCount = MyGlobalConfig.getDrawEachArrayNumber(allCount);

		for (int ow = 0; ow < showData.length; ow++)
		{
//			for (int i = 0; i < eachValCount; i++)
//			{
//				showData[ow] += mAllData.sport_rate_Arrary.get(ow * eachValCount + i);
//			}
//			showData[ow] /= eachValCount;
			showData[ow] = mAllData.sport_rate_Arrary.get(ow);
		}

		for (int sv = 0; sv < showData.length; sv++)
		{
			step_x = eachDistanceW * sv;
			step_y = height - showData[sv] * eachY;

			if (last_step_x != -1 && last_step_y != -1)
			{
				canvas.drawLine(last_step_x, last_step_y,
						step_x, step_y, paint);
			}


			last_step_x = step_x;
			last_step_y = step_y;

		}
	}
		
	// 画运动强度曲线
//	private void drawYDQD(Canvas canvas, Paint paint, float width, float height, float sy_begin) {
//		paint.setColor(Color_QD);
//		paint.setStrokeWidth(3);
//
//		float eachY = sy_begin / mMaxYDQDValue;
//		sy_begin -= eachY/2;
//		eachY = (sy_begin+eachY/2) / mMaxYDQDValue;
//
//		float step_x = 0, step_y = 0;
//		float last_step_x = -1, last_step_y = -1;
//
//		float allCount = mAllData.sport_ydqd_Arrary.size();
//
//		float eachDistanceW = width / MyGlobalConfig.getDrawSplitCount(allCount);  // 计算有多少个刻度
//
//		int[] showData = new int[MyGlobalConfig.getDrawSplitCount(allCount)];
//		int eachValCount = MyGlobalConfig.getDrawEachArrayNumber(allCount);
//
//		for (int ow = 0; ow < showData.length; ow++) {
//			for (int i = 0; i < eachValCount; i++) {
//				showData[ow] += mAllData.sport_ydqd_Arrary.get(ow * eachValCount + i);
//			}
//			showData[ow] /= eachValCount;
//		}
//
//		for (int sv = 0; sv < showData.length; sv++) {
//			step_x = eachDistanceW * sv;
//			step_y = sy_begin - showData[sv] * eachY;
//
//			if ((step_y == last_step_y) && showData[sv] != 0 && (sv != showData.length-1))
//				continue;
//
//			if (last_step_x != -1 && last_step_y != -1) {
//				canvas.drawLine(last_step_x, last_step_y,
//						step_x, step_y, paint);
//			}
//
//			last_step_x = step_x;
//			last_step_y = step_y;
//
//		}
//	}
		
		// 画底部提示
//	private void drawBottom(Canvas canvas, Paint paint, float width, float height) {
//		float  bottomEACH = width / 8;
//
//		paint.setColor(Color.BLACK);
//		paint.setStyle(Paint.Style.STROKE);
//		paint.setStrokeWidth(1);
//		canvas.drawRect(bottomEACH, height - 1.8f * textH, bottomEACH*2, height - 1.4f * textH, paint);
//
//		paint.setColor(Color_D);
//		paint.setStyle(Paint.Style.FILL);
//		canvas.drawText(getResources().getString(R.string.PJXL), bottomEACH*2+10, height - 1.3f * textH, paint);
//
//
//		paint.setColor(Color_QD);
//		paint.setStyle(Paint.Style.FILL);
//		paint.setStrokeWidth(1);
//		canvas.drawRect(bottomEACH*4, height - 1.8f * textH, bottomEACH*5, height - 1.4f * textH, paint);
//
//		paint.setColor(Color_D);
//		canvas.drawText(getResources().getString(R.string.yundongqiangdu), bottomEACH*5+10, height - 1.3f * textH, paint);
//	}
	

}
